package com.bt.lms.ui.page.teaching.supervise;

import com.bt.lms.ui.action.teaching.supervise.LookStuBtnAction;
import com.bt.lms.ui.action.teaching.supervise.PageTeachingSuperviseLookCourseResourceBtnAction;
import com.bt.lms.ui.page.sys.utils.MyHBox;
import com.bt.lms.ui.page.sys.utils.MyVBox;
import com.bt.lms.ui.page.teaching.supervise.action.RealTimeClassBtn;
import com.bt.lms.ui.page.teaching.supervise.action.TeachingCheckBtn;
import com.bt.lms.ui.page.teaching.supervise.action.AddTeacherCheckInAction;
import com.bt.lms.ui.vo.PageSkipInfo;
import com.bt.lms.ui.page.teaching.supervise.action.GradeBtn;
import com.bt.lms.ui.page.sys.vo.TeacherCheckInInfo;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

import java.util.ArrayList;
import java.util.List;

/**
 * 教师值机列表
 */
public class PageTeacherCheckInList extends VBox {


    /**
     * 界面跳转数据
     */
    private final PageSkipInfo pageSkipInfo;

    /**
     * 构建教师值机列表界面
     *
     * @param pageSkipInfo 此界面的一些数据（所在容器 + 所在容器中的角标）
     */
    public PageTeacherCheckInList(PageSkipInfo pageSkipInfo) {

        //自己可能用的东东西
        this.setId("PageTeacherCheckInList");
        this.pageSkipInfo = pageSkipInfo;

        //大小
        int wight = 1280;
        this.setPrefWidth(wight);
        this.setPrefHeight(650);

        //布局
        Insets insets = new Insets(0);
        this.setPadding(insets);

        //组件
        //定位区域
        MyHBox locationHBox = new MyHBox();
        locationHBoxInit(locationHBox);
        this.getChildren().add(locationHBox);

        //按钮区域
        MyHBox btnHBox = new MyHBox();
        initButtonHBox(btnHBox);
        this.getChildren().add(btnHBox);

        //列表区域
        MyVBox listVBox = new MyVBox();
        listVBoxInit(listVBox);
        this.getChildren().add(listVBox);

        //搜索区域
        MyHBox searchHBox = new MyHBox();
        searchHBoxInit(searchHBox);
        this.getChildren().add(searchHBox);

    }

    /**
     * 搜索区域初始化
     *
     * @param searchHBox 搜索区域
     */
    private void searchHBoxInit(HBox searchHBox) {

        //数据控制
        int width = 45;
        int sumStrip = 100;//总条数
        int present = 10;//当前页数
        String sumStripStr = "总记录数" + sumStrip + "条";
        String presentStr = "/" + present;

        //布局
        Insets insets = new Insets(10);
        searchHBox.setPadding(insets);
        searchHBox.setSpacing(10);
        searchHBox.setAlignment(Pos.CENTER_RIGHT);

        //组件
        Label firstLab = new Label("« 首页");
        searchHBox.getChildren().add(firstLab);
        Label lastLab = new Label("« 上一页");
        searchHBox.getChildren().add(lastLab);
        Button pageOne = new Button("1");
        searchHBox.getChildren().add(pageOne);
        Button pageTwo = new Button("2");
        searchHBox.getChildren().add(pageTwo);
        Button pageThree = new Button("3");
        searchHBox.getChildren().add(pageThree);
        Label nextLab = new Label("下一页 »");
        searchHBox.getChildren().add(nextLab);
        Label finallyLab = new Label("尾页 »");
        searchHBox.getChildren().add(finallyLab);
        Label sumNumberLab = new Label(sumStripStr);
        searchHBox.getChildren().add(sumNumberLab);
        TextField pageNumberTF = new TextField();
        pageNumberTF.setPrefWidth(width);
        searchHBox.getChildren().add(pageNumberTF);
        Label presentLab = new Label(presentStr);
        searchHBox.getChildren().add(presentLab);
        Button notarizeBtn = new Button("GO");
        searchHBox.getChildren().add(notarizeBtn);

    }

    /**
     * 列表区域初始化
     *
     * @param listVBox 列表区域
     */
    private void listVBoxInit(MyVBox listVBox) {

        //操作按钮初始化（新增值机安排）
        operateBtnInit(listVBox);

        //列表
        //1.建立列表视图
        TableView<TeacherCheckInInfo> tableView = new TableView<>();

        //2.列头
        //2.1 新建列头
        TableColumn<TeacherCheckInInfo, String> nameTC = new TableColumn<>("值机教师");
        TableColumn<TeacherCheckInInfo, String> timeFrameTC = new TableColumn<>("值机时段");
        TableColumn<TeacherCheckInInfo, Integer> durationTC = new TableColumn<>("值机时长");
        TableColumn<TeacherCheckInInfo, String> scheduleTC = new TableColumn<>("值机任务完成度");
        TableColumn<TeacherCheckInInfo, Integer> perSumTC = new TableColumn<>("答疑人数");
        TableColumn<TeacherCheckInInfo, String> stateTC = new TableColumn<>("状态");
        TableColumn<TeacherCheckInInfo, HBox> operateTC = new TableColumn<>("操作");

        //2.2 列头添加到视图
        tableView.getColumns().addAll(nameTC, timeFrameTC, durationTC, scheduleTC, perSumTC, stateTC, operateTC);

        //3.模拟数据
        //3.1 新建多个假数据
        List<TeacherCheckInInfo> teacherCheckInInfos = simulatedData();
        //3.2 格式转化(用户数据集合转换成Javafx组件集合)
        ObservableList<TeacherCheckInInfo> users = FXCollections.observableArrayList(teacherCheckInInfos);
        //3.3 数据添加到表格
        tableView.setItems(users);

        //4.映射（让每一列知道要展示的属性是哪一个）
        nameTC.setCellValueFactory(new PropertyValueFactory<>("name"));
        timeFrameTC.setCellValueFactory(new PropertyValueFactory<>("timeFrame"));
        durationTC.setCellValueFactory(new PropertyValueFactory<>("duration"));
        scheduleTC.setCellValueFactory(new PropertyValueFactory<>("schedule"));
        perSumTC.setCellValueFactory(new PropertyValueFactory<>("perSum"));
        stateTC.setCellValueFactory(new PropertyValueFactory<>("status"));
        operateTC.setCellValueFactory(new PropertyValueFactory<>("operate"));

        //试图添加
        ObservableList<Node> tableChildren = listVBox.getChildren();
        tableChildren.add(tableView);

    }

    /**
     * 列表区域的操作按钮初始化
     *
     * @param listVBox 列表盒子
     */
    private void operateBtnInit(MyVBox listVBox) {

        //组件(操作按钮)
        Button addRoleBtn = new Button("新增值机安排");
        //事件
        AddTeacherCheckInAction addTeacherCheckInAction = new AddTeacherCheckInAction(pageSkipInfo);
        addRoleBtn.setOnAction(addTeacherCheckInAction);
        //按钮内部布局
        Insets addUserBtnInsets = new Insets(10);
        addRoleBtn.setPadding(addUserBtnInsets);
        //操作按钮盒子（让操作按钮有间隔）
        HBox addUserBtnBox = new HBox();
        //操作按钮盒子布局
        Insets addUserBtnBoxInsets = new Insets(10);
        addUserBtnBox.setPadding(addUserBtnBoxInsets);
        //盒子内添加按钮
        addUserBtnBox.getChildren().add(addRoleBtn);
        //添加到列表容器
        ObservableList<Node> addUserChildren = listVBox.getChildren();
        addUserChildren.add(addUserBtnBox);
    }


    /**
     * 定位区域初始化
     *
     * @param locationHBox 定位区域容器
     */
    private void locationHBoxInit(MyHBox locationHBox) {

        //控制数据
        int height = 42;

        //大小
        locationHBox.setPrefHeight(height);

        //布局
        Insets insets = new Insets(5);
        locationHBox.setPadding(insets);
        locationHBox.setSpacing(0);
        locationHBox.setAlignment(Pos.CENTER_LEFT);

        //组件
        Label siteLab = new Label("当前位置：");
        Label oneCatalogLab = new Label("🏠>系统管理");
        Label twoCatalogLab = new Label(">教师值机管理");

        //添加组件
        locationHBox.getChildren().addAll(siteLab, oneCatalogLab, twoCatalogLab);

    }

    /**
     * 列表界面模拟数据
     *
     * @return 模拟好的数据集合
     */
    private List<TeacherCheckInInfo> simulatedData() {

        //数据总集合
        List<TeacherCheckInInfo> teacherCheckInInfos = new ArrayList<>();

        //模拟第一条数据
        oneSimulateData(teacherCheckInInfos);

        //模拟第二条数据
        twoSimulateData(teacherCheckInInfos);

        //模拟第三条数据
        threeSimulateData(teacherCheckInInfos);

        //模拟第四条数据
        fourSimulateData(teacherCheckInInfos);

        //模拟第五条数据
        fiveSimulateData(teacherCheckInInfos);

        //返回全部数据
        return teacherCheckInInfos;
    }

    /**
     * 第五条数据模拟
     *
     * @param teacherCheckInInfos 总数据集合
     */
    private void fiveSimulateData(List<TeacherCheckInInfo> teacherCheckInInfos) {

        //第五条
        //界面数据对象
        TeacherCheckInInfo teacherCheckInInfo = new TeacherCheckInInfo();
        teacherCheckInInfo.setState("已取消");
        teacherCheckInInfos.add(teacherCheckInInfo);
    }

    /**
     * 第四条数据模拟
     *
     * @param teacherCheckInInfos 总数据集合
     */
    private void fourSimulateData(List<TeacherCheckInInfo> teacherCheckInInfos) {

        //第四条
        //界面数据对象
        TeacherCheckInInfo teacherCheckInInfo = new TeacherCheckInInfo();
        teacherCheckInInfo.setState("已取消");
        teacherCheckInInfos.add(teacherCheckInInfo);
    }


    /**
     * 第三条数据模拟
     *
     * @param teacherCheckInInfos 总数据集合
     */
    private void threeSimulateData(List<TeacherCheckInInfo> teacherCheckInInfos) {

        //第三条
        //界面数据对象
        TeacherCheckInInfo teacherCheckInInfo = new TeacherCheckInInfo();
        teacherCheckInInfo.setState("已过期");
        teacherCheckInInfos.add(teacherCheckInInfo);
    }

    /**
     * 第二条数据模拟
     *
     * @param teacherCheckInInfos 总数据集合
     */
    private void twoSimulateData(List<TeacherCheckInInfo> teacherCheckInInfos) {

        //第二条
        //界面数据对象
        TeacherCheckInInfo teacherCheckInInfo = new TeacherCheckInInfo();
        teacherCheckInInfo.setSchedule("59%");
        teacherCheckInInfo.setPerSum(5);
        teacherCheckInInfo.setState("进行中");
        teacherCheckInInfos.add(teacherCheckInInfo);
    }

    /**
     * 第一条数据模拟
     *
     * @param teacherCheckInInfos 总数据集合
     */
    private void oneSimulateData(List<TeacherCheckInInfo> teacherCheckInInfos) {

        //第一条
        //操作按钮盒子
        HBox operateHBox = operateHBoxInit();
        //界面数据对象
        TeacherCheckInInfo teacherCheckInInfo = new TeacherCheckInInfo("张老师", "2023/5/4 10:00-12:00",
                120, null, null, "未开始", operateHBox);
        teacherCheckInInfos.add(teacherCheckInInfo);
    }

    /**
     * 操作按钮盒子初始化
     *
     * @return 操作按钮盒子
     */
    private HBox operateHBoxInit() {

        //新建操作盒子
        HBox operateHBox = new HBox();
        operateHBox.setPadding(new Insets(2));
        operateHBox.setSpacing(10);

        //操作按钮(修改
        Label amendLab = new Label("修改");
        operateHBox.getChildren().add(amendLab);

        //操作按钮(取消
        Label cancelLab = new Label("取消");
        operateHBox.getChildren().add(cancelLab);

        return operateHBox;
    }

    /**
     * 按钮横向盒子
     *
     * @param buttonHBox 按钮盒子
     */
    private void initButtonHBox(HBox buttonHBox) {

        buttonHBox.setSpacing(20);
        buttonHBox.setPadding(new Insets(5));
        //查看学员按钮
        Button lookStuBtn = new Button("查看学员");
        LookStuBtnAction lookStuBtnAction = new LookStuBtnAction();
        lookStuBtn.setOnAction(lookStuBtnAction);

        //查看课程资源按钮
        Button lookCourseResourceBtn = new Button("查看课程资源");
        ObservableList<Node> children = buttonHBox.getChildren();
        children.addAll(lookStuBtn, lookCourseResourceBtn);
        //查看课程资源按钮事件
        PageTeachingSuperviseLookCourseResourceBtnAction pageTeachingSuperviseLookCourseResourceBtnAction = new PageTeachingSuperviseLookCourseResourceBtnAction();
        lookCourseResourceBtn.setOnAction(pageTeachingSuperviseLookCourseResourceBtnAction);

        //教师值机按钮
        Button teachingCheckBtn = new Button("教师值机管理");
        ObservableList<Node> teachingCheckBtnChildren = buttonHBox.getChildren();
        teachingCheckBtnChildren.add(teachingCheckBtn);
        //事件
        TeachingCheckBtn teachingCheckBtnAction = new TeachingCheckBtn();
        teachingCheckBtn.setOnAction(teachingCheckBtnAction);

        //实时课堂按钮
        Button realTimeClassBtn = new Button("实时课堂管理");
        ObservableList<Node> realTimeClassBtnChildren = buttonHBox.getChildren();
        realTimeClassBtnChildren.add(realTimeClassBtn);
        RealTimeClassBtn realTimeClassBtnAction = new RealTimeClassBtn();
        realTimeClassBtn.setOnAction(realTimeClassBtnAction);

        //成绩及结业按钮
        Button gradeBtn = new Button("成绩及结业管理");
        ObservableList<Node> gradeBtnChildren = buttonHBox.getChildren();
        gradeBtnChildren.add(gradeBtn);
        GradeBtn gradeBtnBtnAction = new GradeBtn();
        gradeBtn.setOnAction(gradeBtnBtnAction);

    }

}
